package com.sskj.run.ui.fragment;

import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bulong.rudeness.RudenessScreenHelper;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.sskj.common.base.App;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseFragment;
import com.sskj.run.R;
import com.sskj.run.R2;
import com.sskj.run.adapter.ExpandAdapter;
import com.sskj.run.adapter.Level0Item;
import com.sskj.run.adapter.Level1Item;
import com.sskj.run.bean.FaqBean;
import com.sskj.run.presenter.FAQFragmentPresenter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Flowable;

/**
 * Created by QinKang on 2018/12/29.
 */

@Route(path = RConfig.RUN_FRAGMENT_FAQ)//常见问题
public class FAQFragment extends BaseFragment<FAQFragmentPresenter> {
    @BindView(R2.id.recyclerView)
    RecyclerView recyclerView;

    private ExpandAdapter expandAdapter;
    private ArrayList<MultiItemEntity> list = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.run_fragment_faq;
    }

    @Override
    public FAQFragmentPresenter getPresenter() {
        return new FAQFragmentPresenter();
    }


    @Override
    public void initView() {
        mPresenter.getFAQ();
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(getActivity())
                .setPaintWidth((int) RudenessScreenHelper.pt2px(App.INSTANCE, 20))
                .setDividerColor(ContextCompat.getColor(App.INSTANCE, R.color.transparent))
        );
        expandAdapter = new ExpandAdapter(list);
        recyclerView.setAdapter(expandAdapter);
    }

    public void updataUI(List<FaqBean> data) {
        list.clear();

        Flowable.fromIterable(data)
                .subscribe(faqBean -> {
                    Level0Item level0Item = new Level0Item(faqBean.getTitle());
                    level0Item.addSubItem(new Level1Item(faqBean.getContent(), faqBean.getContent()));
                    list.add(level0Item);
                });
        expandAdapter.notifyDataSetChanged();

    }


}
